#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mysql.h"

#define HOST           "localhost"
#define MYSQL_USER     "root"
#define MYSQL_PWD      "123456"
#define MYSQL_DB       "test"

#define USERNAME_SIZE  50
#define PWD_SIZE       250

#define SQL_SIZE       1024

#define CHOICE_SIZE    4

#define NOTE_SIZE      150
#define USERID_SIZE    12
#define NOTEID_SIZE    12

int main()
{
 MYSQL *sock;
 MYSQL_RES *res, *res_in;
 MYSQL_FIELD *fd;
 MYSQL_ROW row, note;
 char sql[SQL_SIZE];
 char username[USERNAME_SIZE];
 char pwd[PWD_SIZE];

 while (1) {
       printf("username: ");
       scanf("%s", username);
       printf("password: ");
       scanf("%s", pwd);

       sock = mysql_init(NULL);
       if (!mysql_real_connect(sock, HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB, 0, NULL, 0 )) {
         fprintf(stderr, "%s\n", mysql_error(sock));
         exit(1);
       }

       sprintf(sql, "select userid from n_user where username = '%s' and password = '%s' limit 1", username, pwd);

       if (mysql_query(sock, sql)) {
         fprintf(stderr, "%s\n", mysql_error(sock));
         exit(1);
       }

       res = mysql_use_result(sock);
       if ((row = mysql_fetch_row(res)) == NULL) {
         printf("\nusername or password wrong!\nTry again!\n\n");
       } else {
         printf("Login success!\n\n");
         break;
       }

 }

 char choice[CHOICE_SIZE];
 char buf[NOTE_SIZE];
 time_t t;
 char *uid;
 char userid[USERID_SIZE];
 uid = row[0];
 *userid = *uid;
 mysql_free_result(res);

 while (1) {
       printf("What do you want?\t(1 for note, 2 for show, 3 for delete, 0 for quit)\n");
       scanf("%s", choice);
       printf("\n");

       if (strcmp(choice, "1") == 0) {
         printf("Now note:\n");
         scanf("%s", buf);

         t = time(0);

         sprintf(sql, "insert into n_notebook (userid, note, deleted, time) values('%s', '%s', 0, '%ld')", userid, buf, t);

         if (mysql_query(sock, sql)) {
               fprintf(stderr, "%s\n", mysql_error(sock));
               exit(1);
         }

         printf("Continue?\t(n for no)\n");
         scanf("%s", choice);
         if (strcmp(choice, "n") == 0) {
               return 0;
         }
       } else if (strcmp(choice, "2") == 0) {
         sprintf(sql, "select note,time from n_notebook where userid = %s and deleted = 0", userid);

         if (mysql_query(sock, sql)) {
               fprintf(stderr, "%s\n", mysql_error(sock));
               exit(1);
         }

         unsigned int num_rows;
         unsigned int num = 1;
         res_in = mysql_store_result(sock);
         num_rows = mysql_num_rows(res_in);
         while ((note = mysql_fetch_row(res_in)) != NULL) {
               t = strtol(note[1], NULL, 0);
               struct tm *ltime = localtime(&t);
               printf("%d.%s\t%d-%d-%d %d:%d:%d\n", num, note[0], ltime->tm_year+1900, ltime->tm_mon+1, ltime->tm_mday, ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
               num++;
         }
         mysql_free_result(res_in);

         printf("Continue?\t(n for no)\n");
         scanf("%s", choice);
         if (strcmp(choice, "n") == 0) {
               return 0;
         }
       } else if (strcmp(choice, "3") == 0) {
         sprintf(sql, "select nid,note,time from n_notebook where userid = '%s' and deleted = 0", userid);

         if (mysql_query(sock, sql)) {
               fprintf(stderr, "%s\n", mysql_error(sock));
               exit(1);
         }

         unsigned int num_rows;
         res_in = mysql_store_result(sock);
         num_rows = mysql_num_rows(res_in);
         while ((note = mysql_fetch_row(res_in)) != NULL) {
               t = strtol(note[2], NULL, 0);
               struct tm *ltime = localtime(&t);
               printf("%s.%s\t%d-%d-%d %d:%d:%d\n", note[0], note[1], ltime->tm_year+1900, ltime->tm_mon+1, ltime->tm_mday, ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
         }
         mysql_free_result(res_in);

         char num_of_note[NOTEID_SIZE];
         printf("Please input the number of note to delete!\n");
         scanf("%s", num_of_note);

         sprintf(sql, "update n_notebook set deleted = 1 where nid = '%s'", num_of_note);

         if (mysql_query(sock, sql)) {
               fprintf(stderr, "%s\n", mysql_error(sock));
               exit(1);
         }

       } else if (strcmp(choice, "0") == 0) {
         return 0;
       } else {
         printf("please input 1 or 2 or 0!");
       }

 }

 mysql_close(sock);

}
